package cn.gemiman.daoyi.module.iot.service.jhlequipmentplace;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace.vo.JhlEquipmentPlacePageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace.vo.JhlEquipmentPlaceSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.jhlequipmentplace.JhlEquipmentPlaceDO;
import cn.gemiman.daoyi.module.iot.dal.mysql.jhlequipmentplace.JhlEquipmentPlaceMapper;
import cn.gemiman.daoyi.module.iot.open.jhl.InnoprojhlOpenApiService;
import cn.gemiman.daoyi.module.iot.open.jhl.vo.PlaceQueryReqVO;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.iot.enums.ErrorCodeConstants.JHL_EQUIPMENT_PLACE_NOT_EXISTS;

/**
 * 精华隆场所信息 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class JhlEquipmentPlaceServiceImpl implements JhlEquipmentPlaceService {

    @Resource
    private JhlEquipmentPlaceMapper jhlEquipmentPlaceMapper;
    @Resource
    private InnoprojhlOpenApiService innoprojhlOpenApiService;

    @Override
    public Integer asyncJhlEquipmentPlace() {
        PlaceQueryReqVO reqVO = new PlaceQueryReqVO();
        reqVO.setPageNum(1);
        reqVO.setPageSize(10000);
        int sum = 0;
        boolean hasNext;
        do {
            JSONObject resp = innoprojhlOpenApiService.queryPlace(reqVO);
            if (resp.getInt("code") == 200) {
                JSONObject data = resp.getJSONObject("data");
                hasNext = data.getInt("pages") > data.getInt("current");
                JSONArray records = data.getJSONArray("records");
                sum += records.size();
                records.forEach(record -> {
                    JSONObject place = (JSONObject) record;
                    JhlEquipmentPlaceDO bean = place.toBean(JhlEquipmentPlaceDO.class);
                    if (jhlEquipmentPlaceMapper.selectById(bean.getId()) == null) {
                        jhlEquipmentPlaceMapper.insert(bean);
                    } else {
                        jhlEquipmentPlaceMapper.updateById(bean);
                    }
                });
            } else {
                hasNext = false;
            }
        } while (hasNext);
        return sum;
    }

    @Override
    public Integer createJhlEquipmentPlace(JhlEquipmentPlaceSaveReqVO createReqVO) {
        // 插入
        JhlEquipmentPlaceDO jhlEquipmentPlace = BeanUtils.toBean(createReqVO, JhlEquipmentPlaceDO.class);
        jhlEquipmentPlaceMapper.insert(jhlEquipmentPlace);
        // 返回
        return jhlEquipmentPlace.getId();
    }

    @Override
    public void updateJhlEquipmentPlace(JhlEquipmentPlaceSaveReqVO updateReqVO) {
        // 校验存在
        validateJhlEquipmentPlaceExists(updateReqVO.getId());
        // 更新
        JhlEquipmentPlaceDO updateObj = BeanUtils.toBean(updateReqVO, JhlEquipmentPlaceDO.class);
        jhlEquipmentPlaceMapper.updateById(updateObj);
    }

    @Override
    public void deleteJhlEquipmentPlace(Integer id) {
        // 校验存在
        validateJhlEquipmentPlaceExists(id);
        // 删除
        jhlEquipmentPlaceMapper.deleteById(id);
    }

    private void validateJhlEquipmentPlaceExists(Integer id) {
        if (jhlEquipmentPlaceMapper.selectById(id) == null) {
            throw exception(JHL_EQUIPMENT_PLACE_NOT_EXISTS);
        }
    }

    @Override
    public JhlEquipmentPlaceDO getJhlEquipmentPlace(Integer id) {
        return jhlEquipmentPlaceMapper.selectById(id);
    }

    @Override
    public PageResult<JhlEquipmentPlaceDO> getJhlEquipmentPlacePage(JhlEquipmentPlacePageReqVO pageReqVO) {
        return jhlEquipmentPlaceMapper.selectPage(pageReqVO);
    }

}
